<template>
	<view class="wrap">
		<view class="key-input">
			<view class="title">输入验证码</view>
			<view class="tips">验证码已发送至 +{{ phoneD.phone }}</view>
			<u-message-input :focus="true" :value="value" @change="change" @finish="finish" mode="bottomLine" :maxlength="maxlength"></u-message-input>
			<text :class="{ error: error }">验证码错误，请重新输入</text>
			<view class="captcha">
				<text :class="{ noCaptcha: show }" @tap="noCaptcha">收不到验证码点这里</text>
				<text :class="{ regain: !show }">{{ second }}秒后重新获取验证码</text>
			</view>
		</view>
		<view><u-toast ref="uToast" /></view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			maxlength: 6,
			value: '',
			second: 60,
			show: false,
			error: false,
			tel: '',
			phoneD: {
				phone: '',
				type: 0,
				code: ''
			}
		};
	},
	computed: {},
	onLoad(options) {
		let _t = this;
		_t.phoneD.phone = options.tel;
		_t.phoneD.type = 2;
		console.log(_t.phoneD.phone);
		this.sendCode(_t.phoneD.phone);
		// this.getCaptcha()
		let interval = setInterval(() => {
			this.second--;
			if (this.second <= 0) {
				this.show = true;
				if (this.value.length != 4) {
					this.error = true;
				}
				clearInterval(interval);
			}
		}, 1000);
	},
	methods: {
		//
		sendCode(tel) {
			let _t = this;
			console.log(_t.phoneD);
			this.$u.api.sendCode(_t.phoneD).then(res => {
				if (res.data.status == 400) {
					this.$refs.uToast.show({
						title: res.data.message,
						type: 'default'
					});
				} else {
					this.$refs.uToast.show({
						title: '验证码发送成功',
						type: 'success'
					});
				}
				const data = res.data;
				console.log('sendCode');
				console.log(data);
			});
		},
		//
		loginCode() {
			this.$u.api.loginCode(this.phoneD).then(res => {
				if(res.data.status == 400){
					this.$refs.uToast.show({
						title: res.data.message,
						type: 'error'
					});
				}else if(res.statusCode == 200){
					this.$refs.uToast.show({
						title: "登录成功",
						type: 'success'
					});
					try {
						uni.setStorageSync('token', res.data.token);
						uni.setStorageSync('userInfo', res.data.user.user);
						this.$u.route({
							type:'switchTab',
							url: '/pages/center/index',
						});
					} catch (e) {
						// error
					}
				}else{
					this.$refs.uToast.show({
						title: "未知错误",
						type: 'error'
					});
				}
				console.log('loginCode');
				console.log(res);
			});
		},
		// 收不到验证码选择时的选择
		noCaptcha() {
			let _t = this;
			uni.showActionSheet({
				itemList: ['重新获取验证码'],
				success: function(res) {
					_t.sendCode(_t.tel);
				},
				fail: function(res) {}
			});
		},
		// change事件侦听
		change(value) {
			console.log('change', value);
		},
		// 输入完验证码最后一位执行
		finish(value) {
			console.log('finish', value);
			this.phoneD.code = value;
			this.loginCode();
		}
	}
};
</script>

<style lang="scss" scoped>
.wrap {
	padding: 40rpx;
}

.box {
	margin: 30rpx 0;
	font-size: 30rpx;
	color: 555;
}

.key-input {
	padding: 30rpx 0;
	text {
		display: none;
	}
	.error {
		display: block;
		color: red;
		font-size: 30rpx;
		margin: 20rpx 0;
	}
}

.title {
	font-size: 50rpx;
	color: #333;
}

.key-input .tips {
	font-size: 30rpx;
	color: #333;
	margin-top: 20rpx;
	margin-bottom: 60rpx;
}
.captcha {
	color: $u-type-warning;
	font-size: 30rpx;
	margin-top: 40rpx;
	.noCaptcha {
		display: block;
	}
	.regain {
		display: block;
	}
}
</style>
